package academic.emailClient.view;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;

public class CreateNewTemplateView  extends JPanel {

	private JTabbedPane tabbedPane;
	private JPanel tab1panel;
	private JPanel panel1_buttons;
	private JPanel panel2_buttons;
	private JPanel tab2panel;
	private JPanel tab3panel;
	private ImageIcon iconAdd;
	private ImageIcon iconDelete;
	private JButton tab1createTemplateButton;
	private JButton tab1cancelTemplateButton;
	private JButton tab2saveTemplateButton;
	private JButton tab2cancelTemplateButton;
	private JButton tab2deleteTemplateButton;
	private JTextArea tab1letter;
	private JTextArea tab2letter;
	private JScrollPane tab1letterScroller;
	private JScrollPane tab2letterScroller;
	private JComboBox modifyTemplatescb;
	private JLabel templateNameLabel = new JLabel("Template Name ");
	private JTextField templateNameTextField=new JTextField();
	private JPanel templateNamePanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3));

	/**
	 * Constructor
	 */
	public CreateNewTemplateView() {

		super(new GridLayout(1, 1));
		tab1panel = new JPanel();
		tab2panel = new JPanel();
		tab3panel = new JPanel();
		tabbedPane = new JTabbedPane();
		tab2letter  = new JTextArea();
		//Load combo box with available template files
		modifyTemplatescb = new JComboBox();
		updateModifyTemplateComboBox();
		tab1cancelTemplateButton = new JButton(" Cancel ");
		tab1createTemplateButton = new JButton(" Create ");
		tab2saveTemplateButton = new JButton(" Modify ");	       
		tab2cancelTemplateButton = new JButton(" Cancel ");
		tab2deleteTemplateButton = new JButton(" Delete ");       
		tab1createTemplateButton.setMnemonic(KeyEvent.VK_R);
		tab1cancelTemplateButton.setMnemonic(KeyEvent.VK_C);
		tab2saveTemplateButton.setMnemonic(KeyEvent.VK_M);
		tab2cancelTemplateButton.setMnemonic(KeyEvent.VK_C);
		tab2deleteTemplateButton.setMnemonic(KeyEvent.VK_D);
		tab1panel.setLayout(new BoxLayout(tab1panel,BoxLayout.Y_AXIS));
		tab1panel.setPreferredSize(new Dimension(750, 550));
		tab2panel.setLayout(new BoxLayout(tab2panel,BoxLayout.Y_AXIS));	 
		tab2panel.setPreferredSize(new Dimension(750, 550));
		panel1_buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 3,3));
		panel1_buttons.setSize(400, 50);
		panel1_buttons.add(tab1createTemplateButton);
		panel1_buttons.add(tab1cancelTemplateButton);
		panel2_buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 3,3));
		panel2_buttons.setSize(400, 50);
		panel2_buttons.add(tab2saveTemplateButton);
		panel2_buttons.add(tab2cancelTemplateButton);
		panel2_buttons.add(tab2deleteTemplateButton);	        
		iconAdd = createImageIcon("icons/add.png");
		iconDelete = createImageIcon("icons/delete.png");
		tab1letter = new JTextArea ();
		tab1letter.setSize(900, 600);
		tab1letterScroller = new JScrollPane(tab1letter);
		tab1letterScroller.setPreferredSize(new Dimension(700, 450)); 
		templateNameTextField.setPreferredSize(new Dimension(275, 25)); 
		templateNamePanel.add(templateNameLabel);
		templateNamePanel.add(templateNameTextField);
		templateNamePanel.setMaximumSize( templateNamePanel.getPreferredSize() );
		tab1panel.add(templateNamePanel);
		tab1panel.add(tab1letterScroller, BorderLayout.WEST);
		tab1panel.add(panel1_buttons);
		modifyTemplatescb.setMaximumSize( modifyTemplatescb.getPreferredSize() );
		tab2letterScroller = new JScrollPane(tab2letter);
		tab2letterScroller.setPreferredSize(new Dimension(700, 450));
		tab2panel.add(modifyTemplatescb, BorderLayout.CENTER);
		tab2panel.add(tab2letterScroller, BorderLayout.PAGE_START);
		tab2panel.add(panel2_buttons);
		createTabbedPane();
	}
	
	/**
	 * create Tabbed Pane
	 */
	public void createTabbedPane(){
		tabbedPane.addTab("Add Template", iconAdd, tab1panel, "Add Template Tab");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
		tabbedPane.addTab("Modify Template", iconDelete, tab2panel, "Modify Template Tab");
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

		add(tabbedPane);

		//Use scrolling tabs.
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
	}

	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = CreateNewTemplateView.class.getResource(path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	/**
	 * returns file names of all the files in email folders
	 * @return
	 */
	private ArrayList<String> getAllFilesNamesInTemplatesFolder(){
		ArrayList<String> names = new ArrayList<String>();
		names.add( "Select Template");
		int i = 1;
		File folder = new File("Templates");
		for (File  f: folder.listFiles()){
			if(f.getName() != null) {
				names.add(f.getName());
				i++;
			}
		}
		return names;
	}

	/**
	 *Update Modify Template Combo Box 
	 */
	public void updateModifyTemplateComboBox(){
		ArrayList<String> items = getAllFilesNamesInTemplatesFolder();
		if(modifyTemplatescb.getItemCount() != 0){
			//remove contents of combo box.  
			modifyTemplatescb.removeAllItems(); 
		}
		//fill combo box with updated list.  
		for (int i = 0; i< items.size(); i++){
			modifyTemplatescb.addItem(items.get(i));	
		}
		//clear the text field content
		tab2letter.setText("");
		//force the cb to refresh in the tab
		modifyTemplatescb.revalidate();  
		modifyTemplatescb.repaint();  
	}

	/**
	 * Create the TabbedPane
	 */
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}

	/**
	 * add Panel To Tab1
	 * @param panel
	 */
	public void addPanelToTab1(JPanel panel){
		tab1panel.add(panel);	    
	}

	/**
	 * add Panel To Tab2
	 * @param panel
	 */
	public void addPanelToTab2(JPanel panel){
		tab3panel.add(panel);	    
	}

	/**
	 * add Panel To Tab3
	 * @param panel
	 */
	public void addPanelToTab3(JPanel panel){
		tab3panel.add(panel);	   
	}

	/**
	 * get Tab2 Cancel Button
	 * @return
	 */
	public JButton getTab2CancelButton(){
		return tab2cancelTemplateButton;
	}

	/**
	 * get Tab1 Cancel Button
	 * @return
	 */
	public JButton getTab1CancelButton(){
		return tab1cancelTemplateButton;
	}

	/**
	 * get Tab1 Cancel Button
	 * @return
	 */
	public JButton getTab1CreateButton(){
		return tab1createTemplateButton;
	}

	/**
	 * get Tab2 Modify Button
	 * @return
	 */
	public JButton getTab2ModifyButton(){
		return tab2saveTemplateButton;
	}

	/**
	 * get Tab2 Delete Button
	 * @return
	 */
	public JButton getTab2DeleteButton(){
		return tab2deleteTemplateButton;
	}

	/**
	 * get Modify Template ComboBox
	 * @return
	 */
	public JComboBox getModifyTemplateComboBox(){
		return modifyTemplatescb;
	}

	/**
	 * set Modify Template Text
	 * @param text
	 */
	public void setModifyTemplateText(String text){
		tab2letter.setText(text);
	}
	/**
	 * get Template Name From Tab1
	 * @return
	 */
	public String getTemplateNameFromTab1(){
		return templateNameTextField.getText();
	}

	/**
	 * get Template Name From Tab2
	 * @return
	 */
	public String getTemplateNameFromTab2(){
		return modifyTemplatescb.getSelectedItem().toString();
	}

	/**
	 * get Template Content From Tab2
	 * @return
	 */
	public String getTemplateContentFromTab2(){
		return tab2letter.getText();
	}

	/**
	 * get Template Content From Tab1
	 * @return
	 */
	public String getTemplateContentFromTab1(){
		return tab1letter.getText();
	}
	
	/**
	 * empty Create Template Fields
	 */
	public void emptyCreateTemplateFields(){
		tab1letter.setText("");
		templateNameTextField.setText("");
	}
}
